package com.atguigu.customerdemo.controller;

import com.atguigu.customerdemo.bean.Customer;
import com.atguigu.customerdemo.mapper.CustomerMapper;
import com.atguigu.customerdemo.service.CustomerService;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: CustomerController
 * Package: com.atguigu.customerdemo.controller
 * Description:
 *
 * @Author ChenJun
 * @Create 2023/4/3 12:42
 * @Version 1.0
 */
//@RestController = @Controller + @ResponseBody
@RestController
public class CustomerController {

    @Autowired
    private  CustomerService customerService;

    @Autowired
    private CustomerMapper customerMapper;

    @RequestMapping(value="byName")
    public String getCustomerByName(@RequestParam("name") String name){
//        System.out.println(111);
        String result = customerService.getCustomerByName(name);
        return result;
    }

    @RequestMapping("byId/{id}")
    public String getCustomerById(@PathVariable("id") String id ){
        String byId = customerService.getCustomerById(id);
        return byId;
    }

    //@RequestMapping(value = "111",method = RequestMethod.POST)
    @PostMapping("save")
    public String saveCustomer(@RequestBody Customer customer){
//        customerMapper.insert(customer);
        customerService.saveOrUpdate(customer);
        System.out.println(customer);
        return "success";
    }


    //跳过server层  直接调用方法
    @RequestMapping("saveByNameWithAge")
    public void SaveCustomerByNameWithAge(@RequestParam("name") String name,@RequestParam("age") String age){
        Customer customer = new Customer();
//        customer.setId("3");
//        customer.setName(name);
//        customer.setAge(Integer.parseInt(age));


        //利用Mybatis-plus中Mapper层的方法将数据写入Mysql中
//        customerMapper.insert(customer);
//        customerMapper.saveCustomerByNameAndAge(name,age);
        customerMapper.insert(customer);
        System.out.println("success");
    }


    @PostMapping("InsertCustomer")
    public void insertNameWithAge(@RequestParam("name") String name,@RequestParam("age") String age){
        customerService.insertNameWithAge(name,age);
        System.out.println("通过名字 age添加！");
    }




}
